<?php

/**
 * This is the model class for table "es_users".
 *
 * The followings are the available columns in table 'es_users':
 * @property integer $user_id
 * @property string $username
 * @property string $password
 * @property string $email
 * @property string $birthday
 * @property integer $sex
 * @property integer $scores
 * @property string $balance
 * @property string $consumption
 * @property integer $status
 */
class User extends CActiveRecord
{
	
	const SEX_MALE = 0;
	const SEX_FEMALE = 1;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'es_users';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, password, email, birthday', 'required'),
			array('sex, scores, status', 'numerical', 'integerOnly'=>true),
			array('username', 'length', 'max'=>16),
			array('password', 'length', 'max'=>128),
			array('password_repeat','safe'),
			array('email', 'length', 'max'=>60),
			array('balance, consumption', 'length', 'max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('user_id, username, password, email, birthday, sex, scores, balance, consumption, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'carts'=>array(self::HAS_MANY,'Cart','user_id'),
			'orders'=>array(self::HAS_MANY,'Orders','user_id'),
			'records'=>array(self::HAS_MANY,'Records','user_id'),
			'comments'=>array(self::HAS_MANY,'Comments','user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'user_id' => 'User',
			'username' => 'Username',
			'password' => 'Password',
			'email' => 'Email',
			'birthday' => 'Birthday',
			'sex' => 'Sex',
			'scores' => 'Scores',
			'balance' => 'Balance',
			'consumption' => 'Consumption',
			'status' => 'Status',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('birthday',$this->birthday,true);
		$criteria->compare('sex',$this->sex);
		$criteria->compare('scores',$this->scores);
		$criteria->compare('balance',$this->balance,true);
		$criteria->compare('consumption',$this->consumption,true);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * perform one-way encryption on the password before we store it in the database
	 */
	protected function afterValidate()
	{   
		parent::afterValidate();
		$this->password = $this->encrypt($this->password);                     
	}
	
	public function encrypt($value)
	{
		return md5($value);
	}
	
	/**
	 * @return array sex type names indexed by type IDs
	 */
	public function getSexOptions()
	{
		return array(
				self::SEX_MALE=>'male',
				self::SEX_FEMALE=>'female',
		);
		
	}
	
	/**
	 * @return string the sex text display for the sex
	 */ 
	public function getSexText()
	{
		$typeOptions=$this->typeOptions;
		return isset($typeOptions[$this->type_id]) ? $typeOptions[$this->type_id] : "unknown type ({$this->type_id})";
	}
}